<!DOCTYPE HTML>

<html>
    <head>
        <script type="application/ld+json">
    {
        "@context" : "http://schema.org",
        "@type" : "BlogPosting",
        "mainEntityOfPage": {
             "@type": "WebPage",
             "@id": "\/blog\/"
        },
        "articleSection" : "blog",
        "name" : "jwt访问权限设置",
        "headline" : "jwt访问权限设置",
        "description" : "mff的个人博客",
        "inLanguage" : "en",
        "author" : "",
        "creator" : "",
        "publisher": "",
        "accountablePerson" : "",
        "copyrightHolder" : "",
        "copyrightYear" : "2020",
        "datePublished": "2020-04-28 00:00:00 \x2b0000 UTC",
        "dateModified" : "2020-04-28 00:00:00 \x2b0000 UTC",
        "url" : "\/blog\/blog\/jwt%E8%AE%BF%E9%97%AE%E6%9D%83%E9%99%90%E8%AE%BE%E7%BD%AE\/",
        "wordCount" : "182",
        "keywords" : [ "tutorial","Blog" ]
    }
    </script>
        
            
                <title>jwt访问权限设置</title>
            
        

        <meta charset="utf-8" />
        <meta name="viewport" content="width=device-width, initial-scale=1" />
        <meta name="generator" content="Hugo 0.69.0" />
        
  
    
  

  

  <link rel="apple-touch-icon-precomposed" href='/blog/favicon/apple-touch-icon-precomposed.png'>
  <link rel="icon" href='/blog/favicon/favicon.png'>
  
  <meta name="msapplication-TileColor" content="#da532c">
  <meta name="msapplication-TileImage" content='/favicon/mstile.png'>
  <meta name="application-name" content="mff blog">
  <meta name="msapplication-tooltip" content="">
  <meta name="msapplication-config" content='/favicon/ieconfig.xml'>



        
            <meta name="author" content="mff">
        
        
            <meta name="description" content="mff的个人博客">
        

        <meta name="twitter:card" content="summary"/>
<meta name="twitter:title" content="jwt访问权限设置"/>
<meta name="twitter:description" content="mff的个人博客"/>

        <meta property="og:title" content="jwt访问权限设置" />
<meta property="og:description" content="mff的个人博客" />
<meta property="og:type" content="article" />
<meta property="og:url" content="/blog/blog/jwt%E8%AE%BF%E9%97%AE%E6%9D%83%E9%99%90%E8%AE%BE%E7%BD%AE/" />
<meta property="article:published_time" content="2020-04-28T00:00:00+00:00" />
<meta property="article:modified_time" content="2020-04-28T00:00:00+00:00" />

        <meta property="og:image" content="/blog//images/logo.png">
        <meta property="og:image:type" content="image/png">
        <meta property="og:image:width" content="512">
        <meta property="og:image:height" content="512">
        <meta itemprop="name" content="jwt访问权限设置">
<meta itemprop="description" content="mff的个人博客">
<meta itemprop="datePublished" content="2020-04-28T00:00:00&#43;00:00" />
<meta itemprop="dateModified" content="2020-04-28T00:00:00&#43;00:00" />
<meta itemprop="wordCount" content="182">



<meta itemprop="keywords" content="tutorial," />
        

        
            
        

        
        
            <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/9.12.0/styles/atom-one-light.min.css">
            <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Raleway:400,800,900|Source+Sans+Pro:400,700">
            <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css">
            <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/fancybox/3.1.25/jquery.fancybox.min.css">
            <link rel="stylesheet" href="/blog/css/main.css">
            <link rel="stylesheet" href="/blog/css/add-on.css">
            <link rel="stylesheet" href="/blog/css/academicons.min.css">
        

        
            
                
            
        


  
    
    <link href='//cdn.bootcss.com/highlight.js/9.11.0/styles/github.min.css' rel='stylesheet' type='text/css' />
  


      
    </head>
    <body>

      
      <div id="wrapper">

    
    
<header id="header">
    
      <h1><a href="/blog/">blog</a></h1>
    

    <nav class="links">
        <ul>
            
                <li>
                    <a href="/blog/">
                            <i class="fa fa-home">&nbsp;</i>主页
                    </a>
                </li>
            
                <li>
                    <a href="/blog/blog/">
                            <i class="fa fa-newspaper-o">&nbsp;</i>Blog笔记
                    </a>
                </li>
            
                <li>
                    <a href="https://mff-project.gitee.io/hugoblog/posts/%E4%B8%AA%E4%BA%BA%E7%AE%80%E5%8E%86/">
                            <i class="fa fa-id-card-o">&nbsp;</i>个人简历
                    </a>
                </li>
            
                <li>
                    <a href="/blog/itemized/">
                            <i class="fa fa-list">&nbsp;</i>最近项目
                    </a>
                </li>
            
                <li>
                    <a href="/blog/contact/">
                            <i class="fa fa-envelope-o">&nbsp;</i>联系我
                    </a>
                </li>
            
        </ul>
    </nav>
    <nav class="main">
        <ul>
            
            <li id="share-nav" class="share-menu" style="display:none;">
                <a class="fa-share-alt" href="#share-menu">Share</a>
            </li>
            
            <li class="search">
                <a class="fa-search" href="#search">Search</a>
                <form id="search" method="get" action="//google.com/search">
                    <input type="text" name="q" placeholder="Search" />
                    <input type="hidden" name="as_sitesearch" value="/blog/">
                </form>
            </li>
            <li class="menu">
                <a class="fa-bars" href="#menu">Menu</a>
            </li>
        </ul>
    </nav>
</header>


<section id="menu">

    
        <section>
            <form class="search" method="get" action="//google.com/search">
                <input type="text" name="q" placeholder="Search" />
                <input type="hidden" name="as_sitesearch" value="/blog/">
            </form>
        </section>

    
        <section>
            <ul class="links">
                
                    <li>
                        <a href="/blog/">
                            <h3>
                                <i class="fa fa-home">&nbsp;</i>主页
                            </h3>
                        </a>
                    </li>
                
                    <li>
                        <a href="/blog/blog/">
                            <h3>
                                <i class="fa fa-newspaper-o">&nbsp;</i>Blog笔记
                            </h3>
                        </a>
                    </li>
                
                    <li>
                        <a href="https://mff-project.gitee.io/hugoblog/posts/%E4%B8%AA%E4%BA%BA%E7%AE%80%E5%8E%86/">
                            <h3>
                                <i class="fa fa-id-card-o">&nbsp;</i>个人简历
                            </h3>
                        </a>
                    </li>
                
                    <li>
                        <a href="/blog/itemized/">
                            <h3>
                                <i class="fa fa-list">&nbsp;</i>最近项目
                            </h3>
                        </a>
                    </li>
                
                    <li>
                        <a href="/blog/contact/">
                            <h3>
                                <i class="fa fa-envelope-o">&nbsp;</i>联系我
                            </h3>
                        </a>
                    </li>
                
            </ul>
        </section>

    
        <section class="recent-posts">
            <div class="mini-posts">
                <header>
                    <h3>Recent Posts</h3>
                </header>
                

                
                    
                

                
                        <article class="mini-post">
                            <header>
                                <h3><a href="/blog/blog/fastdfs%E9%85%8D%E7%BD%AE%E5%8F%8A%E4%BB%8B%E7%BB%8D/">FastDFS配置及介绍</a></h3>
                                
                                <time class="published" datetime=
                                    '2020-08-27'>
                                    August 27, 2020</time>
                            </header>
                            

                        </article>
                
                        <article class="mini-post">
                            <header>
                                <h3><a href="/blog/blog/orm%E5%B8%B8%E8%A7%84%E6%93%8D%E4%BD%9C%E9%87%8D%E7%82%B9/">orm常规操作</a></h3>
                                
                                <time class="published" datetime=
                                    '2020-07-28'>
                                    July 28, 2020</time>
                            </header>
                            

                        </article>
                
                        <article class="mini-post">
                            <header>
                                <h3><a href="/blog/blog/%E6%94%AF%E4%BB%98%E5%AE%9D%E6%B2%99%E7%AE%B1%E6%94%AF%E4%BB%98%E6%93%8D%E4%BD%9C/">支付宝沙箱支付操作</a></h3>
                                
                                <time class="published" datetime=
                                    '2020-07-28'>
                                    July 28, 2020</time>
                            </header>
                            

                        </article>
                
                        <article class="mini-post">
                            <header>
                                <h3><a href="/blog/blog/%E8%85%BE%E8%AE%AF%E7%9F%AD%E4%BF%A1%E5%8F%91%E9%80%81%E9%AA%8C%E8%AF%81%E7%A0%81/">腾讯短信发送验证码</a></h3>
                                
                                <time class="published" datetime=
                                    '2020-07-28'>
                                    July 28, 2020</time>
                            </header>
                            

                        </article>
                
                        <article class="mini-post">
                            <header>
                                <h3><a href="/blog/blog/flask&#43;vue%E5%AE%9E%E7%8E%B0%E5%89%8D%E5%90%8E%E7%AB%AF%E5%88%86%E7%A6%BB/">flask&#43;vue实现前后端分离</a></h3>
                                
                                <time class="published" datetime=
                                    '2020-07-26'>
                                    July 26, 2020</time>
                            </header>
                            

                        </article>
                

                
                    <a href=
                        
                            /blog/
                        
                        class="button">View more posts</a>
                
            </div>
        </section>

    
        
</section>

    <section id="share-menu">
    <section id="social-share-nav">
        <ul class="links">
            <header>
                <h3>Share this post <i class="fa fa-smile-o"></i></h3>
            </header>
            



<li>
  <a href="//twitter.com/share?url=%2fblog%2fblog%2fjwt%25E8%25AE%25BF%25E9%2597%25AE%25E6%259D%2583%25E9%2599%2590%25E8%25AE%25BE%25E7%25BD%25AE%2f&amp;text=jwt%e8%ae%bf%e9%97%ae%e6%9d%83%e9%99%90%e8%ae%be%e7%bd%ae&amp;via=" target="_blank" class="share-btn twitter">
    <i class="fa fa-twitter"></i>
    <p>Twitter</p>
    </a>
</li>




<li>
  <a href="//plus.google.com/share?url=%2fblog%2fblog%2fjwt%25E8%25AE%25BF%25E9%2597%25AE%25E6%259D%2583%25E9%2599%2590%25E8%25AE%25BE%25E7%25BD%25AE%2f" target="_blank" class="share-btn google-plus">
    <i class="fa fa-google-plus"></i>
    <p>Google+</p>
  </a>
</li>





<li>
  <a href="//www.facebook.com/sharer/sharer.php?u=%2fblog%2fblog%2fjwt%25E8%25AE%25BF%25E9%2597%25AE%25E6%259D%2583%25E9%2599%2590%25E8%25AE%25BE%25E7%25BD%25AE%2f" target="_blank" class="share-btn facebook">
    <i class="fa fa-facebook"></i>
    <p>Facebook</p>
    </a>
</li>




<li>
  <a href="//reddit.com/submit?url=%2fblog%2fblog%2fjwt%25E8%25AE%25BF%25E9%2597%25AE%25E6%259D%2583%25E9%2599%2590%25E8%25AE%25BE%25E7%25BD%25AE%2f&amp;title=jwt%e8%ae%bf%e9%97%ae%e6%9d%83%e9%99%90%e8%ae%be%e7%bd%ae" target="_blank" class="share-btn reddit">
    <i class="fa fa-reddit-alien"></i>
    <p>Reddit</p>
  </a>
</li>




<li>
  <a href="//www.linkedin.com/shareArticle?url=%2fblog%2fblog%2fjwt%25E8%25AE%25BF%25E9%2597%25AE%25E6%259D%2583%25E9%2599%2590%25E8%25AE%25BE%25E7%25BD%25AE%2f&amp;title=jwt%e8%ae%bf%e9%97%ae%e6%9d%83%e9%99%90%e8%ae%be%e7%bd%ae" target="_blank" class="share-btn linkedin">
      <i class="fa fa-linkedin"></i>
      <p>LinkedIn</p>
    </a>
</li>




<li>
  <a href="//www.stumbleupon.com/submit?url=%2fblog%2fblog%2fjwt%25E8%25AE%25BF%25E9%2597%25AE%25E6%259D%2583%25E9%2599%2590%25E8%25AE%25BE%25E7%25BD%25AE%2f&amp;title=jwt%e8%ae%bf%e9%97%ae%e6%9d%83%e9%99%90%e8%ae%be%e7%bd%ae" target="_blank" class="share-btn stumbleupon">
    <i class="fa fa-stumbleupon"></i>
    <p>StumbleUpon</p>
  </a>
</li>




<li>
  <a href="//www.pinterest.com/pin/create/button/?url=%2fblog%2fblog%2fjwt%25E8%25AE%25BF%25E9%2597%25AE%25E6%259D%2583%25E9%2599%2590%25E8%25AE%25BE%25E7%25BD%25AE%2f&amp;description=jwt%e8%ae%bf%e9%97%ae%e6%9d%83%e9%99%90%e8%ae%be%e7%bd%ae" target="_blank" class="share-btn pinterest">
    <i class="fa fa-pinterest-p"></i>
    <p>Pinterest</p>
  </a>
</li>




<li>
  <a href="mailto:?subject=Check out this post by mff&amp;body=%2fblog%2fblog%2fjwt%25E8%25AE%25BF%25E9%2597%25AE%25E6%259D%2583%25E9%2599%2590%25E8%25AE%25BE%25E7%25BD%25AE%2f" target="_blank" class="share-btn email">
    <i class="fa fa-envelope"></i>
    <p>Email</p>
  </a>
</li>


        </ul>
    </section>
</section>

    
    <div id="main">
        
        
        <article class="post">
  <header>
    <div class="title">
        
            <h1><a href="/blog/blog/jwt%E8%AE%BF%E9%97%AE%E6%9D%83%E9%99%90%E8%AE%BE%E7%BD%AE/">jwt访问权限设置</a></h1>
            
        
        
            <p>mff的个人博客</p>
        
    </div>
    <div class="meta">
        

        <time class="published"
            datetime='2020-04-28'>
            April 28, 2020</time>
        <span class="author">mff</span>
        
            <p>1 minute read</p>
        
        
    </div>
</header>


  
    <section id="social-share">
      <ul class="icons">
        



<li>
  <a href="//twitter.com/share?url=%2fblog%2fblog%2fjwt%25E8%25AE%25BF%25E9%2597%25AE%25E6%259D%2583%25E9%2599%2590%25E8%25AE%25BE%25E7%25BD%25AE%2f&amp;text=jwt%e8%ae%bf%e9%97%ae%e6%9d%83%e9%99%90%e8%ae%be%e7%bd%ae&amp;via=" target="_blank" class="share-btn twitter">
    <i class="fa fa-twitter"></i>
    <p>Twitter</p>
    </a>
</li>




<li>
  <a href="//plus.google.com/share?url=%2fblog%2fblog%2fjwt%25E8%25AE%25BF%25E9%2597%25AE%25E6%259D%2583%25E9%2599%2590%25E8%25AE%25BE%25E7%25BD%25AE%2f" target="_blank" class="share-btn google-plus">
    <i class="fa fa-google-plus"></i>
    <p>Google+</p>
  </a>
</li>





<li>
  <a href="//www.facebook.com/sharer/sharer.php?u=%2fblog%2fblog%2fjwt%25E8%25AE%25BF%25E9%2597%25AE%25E6%259D%2583%25E9%2599%2590%25E8%25AE%25BE%25E7%25BD%25AE%2f" target="_blank" class="share-btn facebook">
    <i class="fa fa-facebook"></i>
    <p>Facebook</p>
    </a>
</li>




<li>
  <a href="//reddit.com/submit?url=%2fblog%2fblog%2fjwt%25E8%25AE%25BF%25E9%2597%25AE%25E6%259D%2583%25E9%2599%2590%25E8%25AE%25BE%25E7%25BD%25AE%2f&amp;title=jwt%e8%ae%bf%e9%97%ae%e6%9d%83%e9%99%90%e8%ae%be%e7%bd%ae" target="_blank" class="share-btn reddit">
    <i class="fa fa-reddit-alien"></i>
    <p>Reddit</p>
  </a>
</li>




<li>
  <a href="//www.linkedin.com/shareArticle?url=%2fblog%2fblog%2fjwt%25E8%25AE%25BF%25E9%2597%25AE%25E6%259D%2583%25E9%2599%2590%25E8%25AE%25BE%25E7%25BD%25AE%2f&amp;title=jwt%e8%ae%bf%e9%97%ae%e6%9d%83%e9%99%90%e8%ae%be%e7%bd%ae" target="_blank" class="share-btn linkedin">
      <i class="fa fa-linkedin"></i>
      <p>LinkedIn</p>
    </a>
</li>




<li>
  <a href="//www.stumbleupon.com/submit?url=%2fblog%2fblog%2fjwt%25E8%25AE%25BF%25E9%2597%25AE%25E6%259D%2583%25E9%2599%2590%25E8%25AE%25BE%25E7%25BD%25AE%2f&amp;title=jwt%e8%ae%bf%e9%97%ae%e6%9d%83%e9%99%90%e8%ae%be%e7%bd%ae" target="_blank" class="share-btn stumbleupon">
    <i class="fa fa-stumbleupon"></i>
    <p>StumbleUpon</p>
  </a>
</li>




<li>
  <a href="//www.pinterest.com/pin/create/button/?url=%2fblog%2fblog%2fjwt%25E8%25AE%25BF%25E9%2597%25AE%25E6%259D%2583%25E9%2599%2590%25E8%25AE%25BE%25E7%25BD%25AE%2f&amp;description=jwt%e8%ae%bf%e9%97%ae%e6%9d%83%e9%99%90%e8%ae%be%e7%bd%ae" target="_blank" class="share-btn pinterest">
    <i class="fa fa-pinterest-p"></i>
    <p>Pinterest</p>
  </a>
</li>




<li>
  <a href="mailto:?subject=Check out this post by mff&amp;body=%2fblog%2fblog%2fjwt%25E8%25AE%25BF%25E9%2597%25AE%25E6%259D%2583%25E9%2599%2590%25E8%25AE%25BE%25E7%25BD%25AE%2f" target="_blank" class="share-btn email">
    <i class="fa fa-envelope"></i>
    <p>Email</p>
  </a>
</li>


      </ul>
    </section>
  

  

  <div id="content">
    <h2 id="jwtjson--web--token">jwt(json  web  token)</h2>
<p>jwt是前端发送请求向后端获取数据时添加了一个token验证，当用户操作合法时才会获取到token，没有权限时访问不到后端接口数据</p>
<h3 id="1-流程图">1. 流程图</h3>
<p><img src="/blog/img/imgs/jwt%E6%B5%81%E7%A8%8B%E5%9B%BE.jpg" alt="jwt流程图"></p>
<h2 id="2-demo">2. demo</h2>
<ol>
<li>
<h3 id="下载jwt">下载jwt</h3>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-cmd" data-lang="cmd">pip install jwt
</code></pre></div></li>
<li>
<h3 id="生成jwt">生成jwt</h3>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-python" data-lang="python"><span style="color:#f92672">import</span> jwt
<span style="color:#f92672">import</span> datetime
<span style="color:#75715e"># 载荷中加入生命周期概念</span>
playload <span style="color:#f92672">=</span> {
    <span style="color:#75715e"># 过期时间 (当前时间 + 设置的时间).timestamp(强转时间戳)</span>
    <span style="color:#e6db74">&#39;exp&#39;</span> : int((datetime<span style="color:#f92672">.</span>datetime<span style="color:#f92672">.</span>now() <span style="color:#f92672">+</span> datetime<span style="color:#f92672">.</span>timedelta(seconds<span style="color:#f92672">=</span><span style="color:#ae81ff">30</span>))<span style="color:#f92672">.</span>timestamp()),
    <span style="color:#e6db74">&#39;data&#39;</span>:{<span style="color:#e6db74">&#39;uid&#39;</span>:<span style="color:#ae81ff">2</span>}
}
<span style="color:#75715e"># 生成jwt(第一个参数为需要生成token的参数，第二个为密钥(自己设定)，第三个为固定参数(算法))</span>
encode_jwt <span style="color:#f92672">=</span> jwt<span style="color:#f92672">.</span>encode(playload,<span style="color:#e6db74">&#39;mff-project&#39;</span>,algorithm<span style="color:#f92672">=</span><span style="color:#e6db74">&#39;HS256&#39;</span>)
<span style="color:#75715e"># 转码</span>
encode_str <span style="color:#f92672">=</span> str(encode_jwt,<span style="color:#e6db74">&#39;utf-8&#39;</span>)
<span style="color:#66d9ef">print</span>(encode_str)
</code></pre></div></li>
<li>
<h3 id="jwt接口测试">jwt接口测试</h3>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-python" data-lang="python"><span style="color:#75715e"># 测试jwt接口</span>
<span style="color:#66d9ef">class</span> <span style="color:#a6e22e">Jwt_test</span>(APIView):
    <span style="color:#66d9ef">def</span> <span style="color:#a6e22e">get</span>(self,request):
        <span style="color:#75715e"># 接收前端的用户id</span>
        uid <span style="color:#f92672">=</span> request<span style="color:#f92672">.</span>GET<span style="color:#f92672">.</span>get(<span style="color:#e6db74">&#39;uid&#39;</span>,None)
        <span style="color:#66d9ef">print</span>(uid)
        <span style="color:#75715e"># 接收前端的token</span>
        myjwt <span style="color:#f92672">=</span> request<span style="color:#f92672">.</span>GET<span style="color:#f92672">.</span>get(<span style="color:#e6db74">&#39;jwt&#39;</span>,None)
        <span style="color:#66d9ef">print</span>(myjwt)
        <span style="color:#75715e"># 验证用户合法性</span>
        <span style="color:#66d9ef">try</span>:
            <span style="color:#75715e"># 解密上传的jwk(这里的密钥要与生成的密钥一致)</span>
            decode_jwt <span style="color:#f92672">=</span> jwt<span style="color:#f92672">.</span>decode(myjwt,<span style="color:#e6db74">&#39;mff-project&#39;</span>,algorithms<span style="color:#f92672">=</span>[<span style="color:#e6db74">&#39;HS256&#39;</span>])
            <span style="color:#66d9ef">print</span>(decode_jwt[<span style="color:#e6db74">&#39;data&#39;</span>][<span style="color:#e6db74">&#39;uid&#39;</span>])
        <span style="color:#66d9ef">except</span> <span style="color:#a6e22e">Exception</span> <span style="color:#66d9ef">as</span> e:
               
            <span style="color:#66d9ef">return</span> Response({<span style="color:#e6db74">&#39;code&#39;</span>:<span style="color:#ae81ff">403</span>,<span style="color:#e6db74">&#39;message&#39;</span>:<span style="color:#e6db74">&#39;无权访问&#39;</span>})
   
        <span style="color:#75715e"># 判断上传的id与生成并解密的id是否一致</span>
        <span style="color:#66d9ef">if</span> int(uid) <span style="color:#f92672">!=</span> int(decode_jwt[<span style="color:#e6db74">&#39;data&#39;</span>][<span style="color:#e6db74">&#39;uid&#39;</span>]):
   
            <span style="color:#66d9ef">return</span> Response({<span style="color:#e6db74">&#39;code&#39;</span>:<span style="color:#ae81ff">403</span>,<span style="color:#e6db74">&#39;message&#39;</span>:<span style="color:#e6db74">&#39;密钥无权限&#39;</span>})
     <span style="color:#75715e"># jwt正确后查询数据库数据</span>
        user <span style="color:#f92672">=</span> User<span style="color:#f92672">.</span>objects<span style="color:#f92672">.</span>get(id<span style="color:#f92672">=</span>int(uid))
     <span style="color:#75715e"># 向前端返回数据</span>
        <span style="color:#66d9ef">return</span> Response({<span style="color:#e6db74">&#39;uid&#39;</span>:uid,<span style="color:#e6db74">&#39;username&#39;</span>:user<span style="color:#f92672">.</span>username,<span style="color:#e6db74">&#39;code&#39;</span>:<span style="color:#ae81ff">200</span>,<span style="color:#e6db74">&#39;message&#39;</span>:<span style="color:#e6db74">&#39;成功&#39;</span>})
</code></pre></div></li>
<li>
<h3 id="前端测试jwt_testvue">前端测试(jwt_test.vue)</h3>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-vue" data-lang="vue">&lt;<span style="color:#f92672">template</span>&gt;
    &lt;<span style="color:#f92672">div</span>&gt;
        &lt;<span style="color:#f92672">p</span>&gt;
            <span style="color:#a6e22e">jwt接口测试</span>
            <span style="color:#a6e22e">测试id</span><span style="color:#960050;background-color:#1e0010">：</span>&lt;<span style="color:#f92672">input</span> <span style="color:#a6e22e">type</span><span style="color:#f92672">=</span><span style="color:#e6db74">&#34;text&#34;</span> <span style="color:#f92672">v-model</span><span style="color:#e6db74">=&#34;uid&#34;</span>&gt;
            &lt;<span style="color:#f92672">br</span>&gt;
            <span style="color:#a6e22e">测试jwt</span><span style="color:#f92672">:</span> &lt;<span style="color:#f92672">input</span> <span style="color:#a6e22e">type</span><span style="color:#f92672">=</span><span style="color:#e6db74">&#34;text&#34;</span> <span style="color:#f92672">v-model</span><span style="color:#e6db74">=&#34;jwt&#34;</span>&gt;
            &lt;<span style="color:#f92672">button</span> <span style="color:#f92672">@click</span><span style="color:#e6db74">=&#34;submit&#34;</span>&gt;<span style="color:#a6e22e">提交</span>&lt;/<span style="color:#f92672">button</span>&gt;
        &lt;/<span style="color:#f92672">p</span>&gt;
    &lt;/<span style="color:#f92672">div</span>&gt;
&lt;/<span style="color:#f92672">template</span>&gt;
   
&lt;<span style="color:#f92672">script</span>&gt;
<span style="color:#66d9ef">export</span> <span style="color:#66d9ef">default</span> {
    <span style="color:#a6e22e">data</span>(){
      <span style="color:#66d9ef">return</span>{
          <span style="color:#a6e22e">uid</span><span style="color:#f92672">:</span><span style="color:#ae81ff">0</span>,
          <span style="color:#a6e22e">jwt</span><span style="color:#f92672">:</span><span style="color:#e6db74">&#39;&#39;</span>,
      }
    },
    <span style="color:#a6e22e">mounted</span><span style="color:#f92672">:</span><span style="color:#66d9ef">function</span>(){
        <span style="color:#75715e">// 获取最新jwt
</span><span style="color:#75715e"></span>    },
    <span style="color:#a6e22e">methods</span><span style="color:#f92672">:</span>{
        <span style="color:#75715e">// 提交测试数据
</span><span style="color:#75715e"></span>        <span style="color:#a6e22e">submit</span><span style="color:#f92672">:</span><span style="color:#66d9ef">function</span>(){
            <span style="color:#66d9ef">this</span>.<span style="color:#a6e22e">axios</span>.<span style="color:#a6e22e">get</span>(<span style="color:#e6db74">&#39;http://127.0.0.1:8000/jwt_test/&#39;</span>,{<span style="color:#a6e22e">params</span><span style="color:#f92672">:</span>{<span style="color:#a6e22e">uid</span><span style="color:#f92672">:</span><span style="color:#66d9ef">this</span>.<span style="color:#a6e22e">uid</span>,<span style="color:#a6e22e">jwt</span><span style="color:#f92672">:</span><span style="color:#66d9ef">this</span>.<span style="color:#a6e22e">jwt</span>}}).<span style="color:#a6e22e">then</span>((<span style="color:#a6e22e">result</span>)=&gt;{
                <span style="color:#a6e22e">console</span>.<span style="color:#a6e22e">log</span>(<span style="color:#a6e22e">result</span>);
                <span style="color:#66d9ef">this</span>.<span style="color:#a6e22e">$Message</span>(<span style="color:#a6e22e">result</span>.<span style="color:#a6e22e">data</span>.<span style="color:#a6e22e">message</span>);
        })           
        },
    }
   
}
&lt;/<span style="color:#f92672">script</span>&gt;
   
&lt;<span style="color:#f92672">style</span>&gt;
   
&lt;/<span style="color:#f92672">style</span>&gt;
</code></pre></div></li>
</ol>
<h2 id="3-用法总结">3. 用法总结</h2>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-python" data-lang="python"><span style="color:#f92672">import</span> jwt
<span style="color:#f92672">import</span> datetime

<span style="color:#75715e"># 载荷中加入生命周期概念</span>
playload <span style="color:#f92672">=</span> {
    <span style="color:#75715e"># 过期时间 (当前时间 + 设置的时间).timestamp(强转时间戳)</span>
    <span style="color:#e6db74">&#39;exp&#39;</span> : int((datetime<span style="color:#f92672">.</span>datetime<span style="color:#f92672">.</span>now() <span style="color:#f92672">+</span> datetime<span style="color:#f92672">.</span>timedelta(seconds<span style="color:#f92672">=</span><span style="color:#ae81ff">30</span>))<span style="color:#f92672">.</span>timestamp()),
    <span style="color:#e6db74">&#39;data&#39;</span>:{<span style="color:#e6db74">&#39;uid&#39;</span>:<span style="color:#ae81ff">2</span>}
}
<span style="color:#75715e"># 生成jwt</span>
encode_jwt <span style="color:#f92672">=</span> jwt<span style="color:#f92672">.</span>encode(playload,<span style="color:#e6db74">&#39;mff-project&#39;</span>,algorithm<span style="color:#f92672">=</span><span style="color:#e6db74">&#39;HS256&#39;</span>)

<span style="color:#75715e"># 转码</span>
encode_str <span style="color:#f92672">=</span> str(encode_jwt,<span style="color:#e6db74">&#39;utf-8&#39;</span>)

<span style="color:#75715e"># 后端解密</span>
decode_jwt <span style="color:#f92672">=</span> jwt<span style="color:#f92672">.</span>decode(encode_str,<span style="color:#e6db74">&#39;mff-project&#39;</span>,algorithms<span style="color:#f92672">=</span>[<span style="color:#e6db74">&#39;HS256&#39;</span>])

<span style="color:#66d9ef">print</span>(encode_str)
<span style="color:#66d9ef">print</span>(decode_jwt[<span style="color:#e6db74">&#39;data&#39;</span>][<span style="color:#e6db74">&#39;uid&#39;</span>])
</code></pre></div>
  </div>

  <footer>
    <ul class="stats">
  <li class="categories">
    <ul>
        
            
            
                <i class="fa fa-folder"></i>
                
                
                <li><a class="article-category-link" href="/blog/blog/categories/hugo">Hugo</a></li>
                
            
        
    </ul>
  </li>
  <li class="tags">
    <ul>
        
            
            
                <i class="fa fa-tags"></i>
                
                
                <li><a class="article-category-link" href="/blog/blog/tags/tutorial">tutorial</a></li>
                
            
        
    </ul>
  </li>
</ul>

  </footer>

</article>

    <article class="post">
        <div id="disqus_thread"></div>
<script type="application/javascript">
    var disqus_config = function () {
    
    
    
    };
    (function() {
        if (["localhost", "127.0.0.1"].indexOf(window.location.hostname) != -1) {
            document.getElementById('disqus_thread').innerHTML = 'Disqus comments not available by default when the website is previewed locally.';
            return;
        }
        var d = document, s = d.createElement('script'); s.async = true;
        s.src = '//' + "shortname" + '.disqus.com/embed.js';
        s.setAttribute('data-timestamp', +new Date());
        (d.head || d.body).appendChild(s);
    })();
</script>
<noscript>Please enable JavaScript to view the <a href="https://disqus.com/?ref_noscript">comments powered by Disqus.</a></noscript>
<a href="https://disqus.com" class="dsq-brlink">comments powered by <span class="logo-disqus">Disqus</span></a>
    </article>


<ul class="actions pagination">
    
        <li><a href="/blog/blog/localstorage%E5%AD%98%E5%82%A8%E6%93%8D%E4%BD%9C/"
                class="button big previous">localStorage存储操作</a></li>
    

    
        <li><a href="/blog/blog/hugo%E7%94%A8%E6%B3%95/"
                class="button big next">hugo用法</a></li>
    
</ul>


    </div>
    
<section id="sidebar">

  
  <section id="intro">
    
    
      
        <a href='/blog/'><img src="/blog/img/main/touxiang3.jpg" class="intro-circle" width="150px" alt="Hugo Future Imperfect" /></a>
      
    
    
      <header>
        <h2>mff</h2>
        <p>Effort is for money, and money is life!</p>
      </header>
    
    
      <ul class="icons">
        
          
    <li><a href="/blog/blog/index.xml" type="application/rss+xml" target="_blank" title="RSS" class="fa fa-rss"></a></li>


        
        












































































  <li><a href="mailto:2980738482@qq.com" title="Email" class="fa fa-envelope"></a></li>


      </ul>
    
  </section>

  
  <section class="recent-posts">
    <div class="mini-posts">
      <header>
        <h3>Recent Posts</h3>
      </header>
      <div class="posts-container">
        

        
          
        

        
          <article class="mini-post">
            <header>
              <h3>
                <a href="/blog/blog/fastdfs%E9%85%8D%E7%BD%AE%E5%8F%8A%E4%BB%8B%E7%BB%8D/">FastDFS配置及介绍</a>
              </h3>
              
              <time class="published" datetime='2020-08-27'>
                August 27, 2020
              </time>
            </header>
            

          </article>
        
          <article class="mini-post">
            <header>
              <h3>
                <a href="/blog/blog/orm%E5%B8%B8%E8%A7%84%E6%93%8D%E4%BD%9C%E9%87%8D%E7%82%B9/">orm常规操作</a>
              </h3>
              
              <time class="published" datetime='2020-07-28'>
                July 28, 2020
              </time>
            </header>
            

          </article>
        
          <article class="mini-post">
            <header>
              <h3>
                <a href="/blog/blog/%E6%94%AF%E4%BB%98%E5%AE%9D%E6%B2%99%E7%AE%B1%E6%94%AF%E4%BB%98%E6%93%8D%E4%BD%9C/">支付宝沙箱支付操作</a>
              </h3>
              
              <time class="published" datetime='2020-07-28'>
                July 28, 2020
              </time>
            </header>
            

          </article>
        
          <article class="mini-post">
            <header>
              <h3>
                <a href="/blog/blog/%E8%85%BE%E8%AE%AF%E7%9F%AD%E4%BF%A1%E5%8F%91%E9%80%81%E9%AA%8C%E8%AF%81%E7%A0%81/">腾讯短信发送验证码</a>
              </h3>
              
              <time class="published" datetime='2020-07-28'>
                July 28, 2020
              </time>
            </header>
            

          </article>
        
          <article class="mini-post">
            <header>
              <h3>
                <a href="/blog/blog/flask&#43;vue%E5%AE%9E%E7%8E%B0%E5%89%8D%E5%90%8E%E7%AB%AF%E5%88%86%E7%A6%BB/">flask&#43;vue实现前后端分离</a>
              </h3>
              
              <time class="published" datetime='2020-07-26'>
                July 26, 2020
              </time>
            </header>
            

          </article>
        
      </div>

      
        <a href=
          
            /blog/
          
        class="button">View more posts</a>
      
    </div>
  </section>

  
  
  
  
  
    <section id="categories">
      <header>
        <h3>
          <a href="/blog/categories/">Categories</a>
        </h3>
      </header>
        
          
        

        
        <p>
          <article>
            <header>
              
                <a href="/blog/categories/hugo/">hugo</a>
                <span style="float:right;">43</span>
              
            </header>
          </article>
        </p>
        
    </section>
  
  

  
  
    <section id="mini-bio">
      <h3>About</h3>
      <p>blog于2018年6月10日创建(It was created on May 10, 2020),文章主要介绍了python的一些常用框架和库及它们的一些用法</p>
      <a href="/blog/about/" class="button">Learn More</a>
    </section>
  

  
  <section id="footer">
    
      <ul class="icons">
        
          
    <li><a href="/blog/blog/index.xml" type="application/rss+xml" target="_blank" title="RSS" class="fa fa-rss"></a></li>


        
        












































































  <li><a href="mailto:2980738482@qq.com" title="Email" class="fa fa-envelope"></a></li>


      </ul>
    
    <p class="copyright">
      
        &copy; 2020
        
          mff blog
        
      .
      Powered by <a href="//gohugo.io" target="_blank">Hugo</a>
    </p>
  </section>
</section>

    </div>
    <a id="back-to-top" href="#" class="fa fa-arrow-up fa-border fa-2x"></a>
    

    
      
    

    
      
      
      
        <script src="//cdn.bootcss.com/highlight.js/9.11.0/highlight.min.js"></script>
        
        
        
        <script src="//cdn.bootcss.com/highlight.js/9.11.0/languages/r.min.js"></script>
        <script src="//cdn.bootcss.com/highlight.js/9.11.0/languages/yaml.min.js"></script>
        <script src="//cdn.bootcss.com/highlight.js/9.11.0/languages/css.min.js"></script>
        <script>hljs.configure({languages: []}); hljs.initHighlightingOnLoad();</script>
      
    
    
    
      <script src="https://code.jquery.com/jquery-3.2.1.min.js" integrity="sha256-hwg4gsxgFZhOsEEamdOYGBf13FyQuiTwlAQgxVSNgt4=" crossorigin="anonymous"></script>
      <script src="https://cdnjs.cloudflare.com/ajax/libs/skel/3.0.1/skel.min.js"></script>
      <script src="https://cdnjs.cloudflare.com/ajax/libs/fancybox/3.1.25/jquery.fancybox.min.js"></script>
      <script src="/blog/js/util.js"></script>
      <script src="/blog/js/main.js"></script>
      <script src="/blog/js/backToTop.js"></script>
    

    
      
        
      
    

    
    <script>hljs.initHighlightingOnLoad();</script>
      <script src="//yihui.name/js/math-code.js"></script>
<script async
src="//cdn.bootcss.com/mathjax/2.7.1/MathJax.js?config=TeX-MML-AM_CHTML">
</script>


  </body>
</html>

